package com.hdoit.ares.web.cust.wechat;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.selfcommon.enums.api.AppResponseStatus;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.webapi.ResponseModel;
import com.tennetcn.web.cbase.AuthPassport;

@Controller
@RequestMapping("/wechat/cust/appShareRegister/")
@AuthPassport
public class appShareRegisterWechat extends BaseController{

	@Override
	public String getRoutePath() {
		return "areas/wechat/cust/appShareRegister/";
	}
	
	private ICustomerService customerService;
	
	public ICustomerService getCustomerService() {
		return customerService;
	}

	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	private final String registeredViewName = "registered";
	private final String registeredNowViewName = "registeredNow";
	private final String downloadViewName = "download";
	
	
	@RequestMapping(value = registeredViewName,method={RequestMethod.GET})
	public String registered(Model model,Integer shareCode){
		model.addAttribute("identity", shareCode);
		return view(registeredViewName);		
	}
	
	@RequestMapping(value = downloadViewName,method={RequestMethod.GET,RequestMethod.POST})
	public String download(Model model){		
		
		return view(downloadViewName);		
	}

	@ResponseBody
	@RequestMapping(value = registeredNowViewName,method = {RequestMethod.POST,RequestMethod.GET},produces="application/json;charset=utf-8")
	public String registeredNow(HttpServletRequest request,String phone,String code,Integer identity){
		ResponseModel rm = new ResponseModel();
		if(!StringUtils.isEmpty(code)){
			String matchCode = String.valueOf(request.getSession().getAttribute("verifyCode"));
			System.out.println(matchCode);
			Date sendTime = (Date)request.getSession().getAttribute("sendTime");
			if(!StringUtils.isEmpty(sendTime)){
				if(sendTime.getTime()-DateUtils.getCurrent().getTime()>5*60*1000){
					rm.setStatus(AppResponseStatus.code_expire);
					rm.setMessage("验证码过期");
					return rm.toJson();
				}
			}	
			if(!code.equals(matchCode)){
				rm.setStatus(AppResponseStatus.code_data_error);
				rm.setMessage("验证码错误");
				return rm.toJson();
			}
			Customer customer = customerService.queryModelByPhone(phone);
			Customer refreCustomer = null;
			if(StringUtils.isEmpty(identity)){
				refreCustomer = customerService.queryModelByUserIdentity(identity);
			}
			if(customer!=null){
				if(customer.getRefereeIdentity()<=0){
					
					customer.setRefereeId(refreCustomer.getId());
					customer.setRefereeIdentity(refreCustomer.getUserIdentity());
					customer.setModelStatus(ModelStatus.update);
					customerService.applyChange(customer);
				}
				rm.setStatus(AppResponseStatus.success);
				rm.setMessage("此手机号已经注册");
				return rm.toJson();
			}else{
				customer = new Customer();
				customer.setId(CommonUtils.getKeyId());
				customer.setPhone(phone);
				customer.setRegistDate(DateUtils.getCurrent());
				int userIdentity = customerService.queryMaxUserIdentity();
				if (userIdentity < 2000) {
					userIdentity = 2000;
				}
				userIdentity++;
				customer.setUserIdentity(userIdentity);
				customer.setModelStatus(ModelStatus.add);
				customer.setRefereeId(refreCustomer.getId());
				customer.setRefereeIdentity(refreCustomer.getUserIdentity());
				customerService.applyChange(customer);
				rm.setStatus(AppResponseStatus.success);
				rm.setMessage("注册成功");
				return rm.toJson();
			}			
		}
		
		rm.setStatus(AppResponseStatus.code_data_error);
		rm.setMessage("验证码错误");
		return rm.toJson();
	}
}
